<template>
	<view>
		<my-search @click="clickSelect" :sticky="true"></my-search>
		<view class="scroll-view-container">
			<scroll-view class="scroll-view-left" scroll-y="true" :style="{height: wh + 'px'}">
				<block v-for="(item, index) in cateList" :key="item.cat_id">
					<view :class="['scroll-view-left-item', active === index ? 'active' : '']" @click="activeChange(index)">
						{{ item.cat_name }}
					</view>
				</block>
			</scroll-view>
			<scroll-view class="scroll-view-right" scroll-y="true" :style="{height: wh + 'px'}">
				<view class="scroll-view-right-main" v-for="level2 in levelCateList" :key="level2.cat_id">
					<view class="header">/&nbsp;{{ level2.cat_name }}&nbsp;/</view>
					<view class="main">
						<view class="main-item" v-for="level3 in level2.children" :key="level3.cat_id" @click="level3Item(level3)">
							<image :src="level3.cat_icon"></image>
							<text>{{level3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 设备可使用窗口高度
				wh: 0,
				// 1级菜单栏数据
				cateList: [],
				// 2级菜单数据
				levelCateList: [],
				// 当前选中项的索引，默认让第一项被选中
				active: 0
			};
		},
		onLoad() {
			// 获取手机信息。
			this.wh = uni.getSystemInfoSync().windowHeight - 50
			// 获取左侧菜单栏数据
			this.getCateList()
		},
		methods: {
			// 获取左侧菜单栏数据
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				if (res.meta.status !== 200) uni.$showMsg()
				this.cateList = res.message
				// 二级菜单
				this.levelCateList = res.message[0].children
				console.log(this.levelCateList);
			},
			// 当前选中的菜单
			activeChange(i) {
				this.active = i
				// 二级菜单
				this.levelCateList = this.cateList[i].children
			},
			// 点击3级分类
			level3Item(level3) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + level3.cat_id
				})
			},
			// 点击搜索
			clickSelect() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.scroll-view-left {
			width: 120px;

			.scroll-view-left-item {
				line-height: 60px;
				text-align: center;
				font-size: 12px;
				background-color: #f7f7f7;

				// 激活选项样式
				&.active {
					position: relative;
					background-color: #ffffff;

					&::before {
						content: "";
						display: block;
						position: absolute;
						top: 50%;
						left: 0;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						transform: translateY(-50%);
					}
				}
			}
		}

		.scroll-view-right {
			background-color: #FFFFFF;
			.scroll-view-right-main {
				.header {
					font-weight: bold;
					font-size: 12px;
					text-align: center;
					padding: 15px 0;
				}
				.main {
					display: flex;
					flex-wrap: wrap;
					padding-bottom: 10rpx;
					.main-item {
						display: flex;
						flex-direction: column;
						align-items: center;
						width: 33.33%;
						image {
							width: 60px;
							height: 60px;
						}
						text {
							font-size: 12px;
						}
					}
				}
			}
		}
	}
</style>
